package com.gopay.common.core.dic.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.dic.SchemaCode;
import com.gopay.common.core.dic.dao.AcpsSchemaAcctRelQueryDAO;
import com.gopay.common.domain.acps.AcpsSchemaAcctRel;

/**
 * 
 * @AcpsSchemaAcctRelQueryDAOImpl.java
 * @author liujy
 * @2013-1-23 下午3:22:57 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("acpsSchemaAcctRelQueryDAO")
public class AcpsSchemaAcctRelQueryDAOImpl extends BaseDAOImpl<AcpsSchemaAcctRel, Serializable> implements
        AcpsSchemaAcctRelQueryDAO {

	@SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<String> getInnerAccts(SchemaCode schemaCode, String gopayIntTxnCd) {
        Map map = new HashMap();
        map.put("schemaCode", schemaCode.value);
        map.put("gopayIntTxnCd", gopayIntTxnCd);
        List<AcpsSchemaAcctRel> acpsSchemaAcctRelList = findByHql(formatHql(map), map);
        List<String> list = new ArrayList<String>();
        if(acpsSchemaAcctRelList != null && acpsSchemaAcctRelList.size() != 0){
        	for(int i = 0 ;i < acpsSchemaAcctRelList.size() ;i++){
        		list.add(acpsSchemaAcctRelList.get(i).getAcctId());
        	}
        }
    	return list;
    }

	
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public String getInnerAcct(SchemaCode schemaCode, String gopayIntTxnCd) {
        Map map = new HashMap();
        map.put("schemaCode", schemaCode.value);
        map.put("gopayIntTxnCd", gopayIntTxnCd);
        List list = findByHql(formatHql(map), map);
        if (list == null || list.size() != 1) {
            return null;
        }
        return ((AcpsSchemaAcctRel) list.get(0)).getAcctId();
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List<AcpsSchemaAcctRel> getInnerAccts(String gopayIntTxnCd) {
        Map map = new HashMap();
        map.put("gopayIntTxnCd", gopayIntTxnCd);
        return findByHql(formatHql(map), map);
    }

    /**
     * 
     * @param param
     * @return
     */
    @SuppressWarnings("rawtypes")
    private String formatHql(Map param) {
        StringBuilder hql = new StringBuilder(" from AcpsSchemaAcctRel where ");
        Iterator iterator = param.keySet().iterator();
        while (iterator.hasNext()) {
            String column = (String) iterator.next();
            hql.append(column);
            hql.append(" =:").append(column);
            hql.append(" and ");
        }
        // 正则表达式说明：\\b单词边界。\\S 空格。 *多个。 $ 从后面匹配。
        // "\\band\\b\\s*$"含义，从后面替换 多个空格，加and单词的字符串，即【 and 】或 【 and】或【 and 】
        // "\\bwhere\\b\\s*$"含义，从后面替换 多个空格，加where单词的字符串，即【 where 】或 【 where】或【
        // where 】
        String strHql = hql.toString().replaceAll("\\band\\b\\s*$", "").replaceAll("\\bwhere\\b\\s*$", "");
        return strHql;
    }
}
